from DrissionPage import Chromium, ChromiumOptions
from DrissionPage.common import Settings
import threadpool, os
import datetime
# 启用多例：
Settings.singleton_tab_obj = False

today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)


def Download(url_list):
    '''下载空调数据'''
    global today, yesterday

    tab = url_list[0]
    Air_conditioning = url_list[1]
    tab.get('http://212.11.9.11:8077/static/#/login')
    userName = tab.ele('@name=username')
    userName.input('admin')
    password = tab.ele('@name=password')
    password.input('admin@123\n')
    tab.wait.load_start()
    tab.get('http://212.11.9.11:8077/static/#/report/realStatistics')
    tab.ele('自定义').click('js')
    tab.ele('t:input@@placeholder=开始时间').input(str(yesterday) + ' 00:00:00\n',
                                               clear=True)
    tab.ele('确定').click('js')
    tab.ele('t:input@@placeholder=结束时间').input(str(today) + ' 00:00:00\n',
                                               clear=True)
    tab.ele('确定').click('js')
    tab.wait(1)
    for i in Air_conditioning:
        search_for = tab.ele(
            'x:/html/body/div/div/div[2]/div[2]/section/div[1]/div/div[1]/div/div[3]/div/input'
        )
        search_for.input(i + '\n\n', clear=True)
        if len(tab.eles('楼')) == 2:
            search_for.input(i + '\n\n', clear=True)
        tab.ele('楼').click('js')
        tab.ele('层').click('js')
        machine_room = tab.ele('机房')
        machine_room.click('js')
        air_conditioning = tab.eles('空调')
        for i in air_conditioning:
            file_name = 'D:\lz\每日空调\\' + str(
                yesterday) + '\\' + i.text + '.xlsx'
            if os.path.exists(file_name):
                # print(i.text + '已下载')
                continue
            i.click('js')
            i.parent(2).ele('t:label').click()
            while len(machine_room.parent(3).s_eles('t:div')) < 10:
                pass
            #下载
            tab.set.when_download_file_exists('skip')
            mission = tab('下载').click.to_download(
                'D:\lz\每日空调\\' + str(yesterday), i.text,
                by_js=True)  # 点击一个会触发下载的链接，同时设置下载路径和文件名
            try:
                mission.wait(180)  # 等待下载结束
            except:
                print(i.text + '下载有问题')
                while not tab('下载').states.is_clickable:
                    tab.wait(1)
            i.parent(2).ele('t:label').click()

def main():
    '''主函数'''
    global today, yesterday

    co = ChromiumOptions().headless()
    page = Chromium(co)
    page_list = [page.new_tab() for i in range(4)]
    air_conditioning = [['2-3空调#1', '2-3空调#2', '2-3空调#3', '2-3空调#4'],
                        ['3-3空调#1', '3-3空调#2', '3-3空调#3'],
                        ['3-4空调#1', '3-4空调#2', '3-4空调#3'],
                        ['4-2空调#1', '4-2空调#2']]

    url_list = [[page_list[i], air_conditioning[i]] for i in range(4)]
    pool = threadpool.ThreadPool(4)
    requests = threadpool.makeRequests(Download, url_list)
    [pool.putRequest(req) for req in requests]
    pool.wait()
    try:
        page.wait.downloads_done()
    except:
        pass
    page.quit()
    file_name_s = os.path.join(f'D:\lz\每日空调\\{yesterday}')
    while len(os.listdir(file_name_s)) < 24:
        print('没有全部下载完成')
        for i in os.listdir(file_name_s):
            if '实时' in i:
                os.remove(os.path.join(file_name_s, i))
        main()
    print('下载完成')


if __name__ == '__main__':
    main()
